package net.example.lesson5.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class RequestHandlerTask implements Runnable {
    private Socket socket;

    RequestHandlerTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());) {
            String result = "";
            String info = null;
            while ((info = br.readLine()) != null) {
//                System.out.println("我是服务器，客户端说："+info);
                result += info;
            }
            socket.shutdownInput();//关闭输入流
            out.println("Hello world! " + Thread.currentThread().getName());
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
